/**
* @Author: zmx
* @Date: 2023/9/26
* @Description:
*/
<template>
  <el-dialog :title="'维护风险算法指标阈值'" :visible.sync="show" append-to-body width="80%" :before-close="handleClose">
    <el-form>
      <el-form-item label="风险算法指标阈值：">
        <el-table :data="tableData" style="width: 100%" :header-cell-style="{background:'#006175',color:'#fff'}">
          <el-table-column prop="itemThresholdMin" label="最小" width="140">
            <template v-slot="scope">
              <el-input v-model="tableData[scope.$index]['itemThresholdMin']" placeholder="请输入最小值" @change="changeT7dMin(scope.$index)"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="itemThresholdMax" label="最大" width="140">
            <template v-slot="scope">
              <el-input v-model="tableData[scope.$index]['itemThresholdMax']" placeholder="请输入最大值" @change="changeT7dMax(scope.$index)"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="itemThresholdPeriod" label="单位" width="130">
            <template v-slot="scope">
              <el-input v-model="tableData[scope.$index]['itemThresholdPeriod']" placeholder="请输入单位"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="itemValue" label="分值" width="130">
            <template v-slot="scope">
              <el-input v-model="tableData[scope.$index]['itemValue']" placeholder="请输入分值" @change="changeT7dValue(scope.$index)"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="itemThresholdDesc" label="说明">
            <template v-slot="scope">
              <el-input v-model="tableData[scope.$index]['itemThresholdDesc']" placeholder="请输入说明"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="100">
            <template v-slot="scope">
              <el-button class="common-btn" @click="addT7d()">+</el-button>
              <el-button class="delete-btn" @click="deleteT7d(scope.$index)" v-show="tableData.length>1">-</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="checkBtn">确 认</el-button>
      <el-button @click="handleClose">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {getRiskItemQueryT7d} from "../../api/risk/riskItem";

export default {
  props: ['T7dDialogShow', 'itemId'], //接受父组件传递过来的数据
  data() {
    return {
      show: false, //弹窗默认隐藏
      tableData: [{itemThresholdMin:'',itemThresholdMax:'',itemThresholdDesc:'',itemThresholdPeriod:'',itemValue:''}]
    };
  },
  mounted() {
    //显示弹窗
    this.getInit()
    this.show = this.T7dDialogShow
  },
  methods: {
    handleClose() {
      //关闭弹窗
      this.show = false
      this.$emit('closeDialog')
    },
    checkBtn() {
      //确定按钮
      if (this.checkParam()) {
        this.$emit('selectData', this.tableData)   //发送数据到父组件
        this.handleClose()
      }
    },
    checkParam() {
      let result = true
      this.tableData.forEach(item => {
        if (!item.itemThresholdMax && !item.itemThresholdMin) {
          this.$message({ message: '请输入最大或者最小值', type: 'warning' })
          result = false
          return
        }
        if (!item.itemThresholdPeriod) {
          this.$message({ message: '请输入单位', type: 'warning' })
          result = false
          return
        }
        if (!item.itemValue) {
          this.$message({ message: '请输入分值', type: 'warning' })
          result = false
        }
      })
      return result
    },
    changeT7dMin(index) {
      let reg = /(^-?[1-9]\d*\.\d+$|^-?0\.\d+$|^-?[1-9]\d*$|^0$)/
      if (!this.tableData[index].itemThresholdMin.match(reg)) {
        this.tableData[index].itemThresholdMin = ''
        this.$message({ message: '请输入数值', type: 'warning' })
      }
    },
    changeT7dMax(index) {
      let reg = /(^-?[1-9]\d*\.\d+$|^-?0\.\d+$|^-?[1-9]\d*$|^0$)/
      if (!this.tableData[index].itemThresholdMax.match(reg)) {
        this.tableData[index].itemThresholdMax = ''
        this.$message({ message: '请输入数值', type: 'warning' })
      }
    },
    changeT7dValue(index) {
      let reg = /(^-?[1-9]\d*\.\d+$|^-?0\.\d+$|^-?[1-9]\d*$|^0$)/
      if (!this.tableData[index].itemValue.match(reg)) {
        this.tableData[index].itemValue = ''
        this.$message({ message: '请输入数值', type: 'warning' })
      }
    },
    changeTable() {
      let tmpContent = ''
      this.tableData.forEach(item => {
        if (item.powerValue && item.itemSymbol && item.itemTypeSymbol) {
          tmpContent += item.itemSymbol + item.itemTypeSymbol + '*' + item.powerValue
        }
      })
      this.formInline.ruleContent = tmpContent
    },
    addT7d() {
      this.tableData.push({itemThresholdMin:'',itemThresholdMax:'',itemThresholdDesc:'',itemThresholdPeriod:'',itemValue:''})
    },
    deleteT7d(index) {
      this.tableData.splice(index, 1)
    },
    async getInit() {
      let params = {
        itemId: this.itemId,
        source: 'web'
      }
      await getRiskItemQueryT7d(params).then((res)=>{
        if(res && res.itemListSize > 0){
          this.tableData = res.itemList
        }
      }).catch(err => {
        this.$message({ message: '服务器异常：' + err, type: 'warning' })
      })
    },
  }
};
</script>

<style scoped>

</style>
